//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    hiddens: true,
    indicatorDots: false,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    allShops:[
      { id: 0, name: "热销推荐", image:"../../image/rexiao.png"},
      { id: 1, name: "上装", image: "../../image/classify2.png" },
      { id: 2, name: "套装", image: "../../image/classify3.png" },
      { id: 3, name: "裙装", image: "../../image/classify4.png" },
      { id: 4, name: "热销商品", image: "../../image/classify5.png" },
      { id: 5, name: "老板推荐", image: "../../image/classify6.png" },
    ]
  },

  onLoad: function (options) {
    var that = this;
    this.setData({
      imageUrl: app.globalData.imageUrl
    })
    app.showShareMenus();
    var scene = decodeURIComponent(options.scene);
    if (scene != 'undefined') {
      app.globalData.referrerId = scene;
      useUserInfo(this);
    }else{
      if (options.id) {
        app.globalData.referrerId = options.id;
        useUserInfo(this);
      } else {
        useUserInfo(this);
      }
    }
  },
  getUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
 turnToSearch: function () {
    wx.navigateTo({
      url: '../../pages/searchShops/searchShops',
    })
  }
  
  , onShow: function () {
    //getShops(this);
    getProducts(this,"热销推荐");
    getProducts(this, "老板推荐");
    getProducts(this, "热销商品");
    getIndexBanner(this)
  },
  onShareAppMessage: function (res) {
    return {
      title: '成都品牌服饰批发零售定制',
      path: '/pages/homepage/homepage?id=' + wx.getStorageSync("userid"),
    }
  }
  , toShopDetail:function(e){
    wx.navigateTo({
      url: '../../pages/allGoods/allGoods?id='+e.target.id,
    })
  }
  , toProductsDetail:function(e){
    wx.navigateTo({
      url: '../../pages/directDetail/directDetail?id=' + e.target.id,
    })
  }
  , toAllShops: function () {
    wx.navigateTo({
      url: '../../pages/moreSeller/moreSeller',
    })
  }
  , toAllGoods:function(){
     wx.navigateTo({
       url: '../../pages/allGoods/allGoods',
     })
  }
})



function getIndexBanner(that) {
  wx.request({
    url: app.globalData.allUrl + "/user/image",
    data: {
    },
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
     // console.log(res.data)
      that.setData({
        imgUrls: res.data
      })
    }
  })
}


function getProducts(that,_id) {
  wx.request({
    url: app.globalData.allUrl + "/goods/goods_class",
    data: {
      types: _id
    },
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
     console.log(res.data)
    if(_id=="热销推荐"){
      that.setData({
        zhoubian: res.data
      })
    } else if (_id == "老板推荐") {
      that.setData({
        guonei: res.data
      })
    } else if (_id == " 热销商品") {
      that.setData({
        chujing: res.data
      })
    }
      
    }
  })
}

function useUserInfo(that) {
  if (wx.getStorageSync('userid')) {
    that.setData({
      userInfo: wx.getStorageSync('userInfo')
    })
  } else {
    //调用登录接口
    wx.login({
      success: function (rese) {
        wx.getUserInfo({
          success: function (res) {
            app.globalData.hasRoot = false;
            app.globalData.noRoot = true;
            wx.setStorageSync('userInfo', res.userInfo)//将用户信息储存在本
            app.globalData.userInfo = res.userInfo;
            var reg = /[A-Za - z0 - 9_\-\u4e00 - \u9fa5]+/
            if (!reg.test(res.userInfo.nickName)) {
              res.userInfo.nickName == "默认用户名"
            }
            that.setData({
              userInfo: res.userInfo
            })
            wx.request({
              url: app.globalData.allUrl + "/login/login",
              data: {
                code: rese.code,
              },
              header: { 'content-type': "application/x-www-form-urlencoded" },
              method: 'POST',
              success: function (datas) {
       //         console.log(datas.data)
                wx.request({
                  url: app.globalData.allUrl + "/user/saveUserInfo",
                  data: {
                    user_id: datas.data.openid,
                    user_name: res.userInfo.nickName,
                    image_url: res.userInfo.avatarUrl,
                  //  pid: app.globalData.referrerId,
                  //  pid:"o6EMU0dmBI65mKguVWmW3eiR7AM1"
                  },
                  header: { 'content-type': "application/x-www-form-urlencoded" },
                  method: 'POST',
                  success: function (res) {
                //    console.log(res)
                    wx.setStorageSync('userid', datas.data.openid)
                  }, fail: function () {
                  }
                })
              }, fail: function () {
              }
            })
          }, fail: function () {
            wx.showModal({
              title: '提示',
              content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',
              success: function (res) {
                if (res.confirm) {
                  wx.openSetting({
                    success: (res) => {
                      if (res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录
                        wx.getUserInfo({
                          success: function (res) {
                            app.globalData.hasRoot = false;
                            app.globalData.noRoot = true;
                            wx.setStorageSync('userInfo', res.userInfo)//将用户信息储存在本
                            app.globalData.userInfo = res.userInfo;
                            var reg = /[A-Za - z0 - 9_\-\u4e00 - \u9fa5]+/
                            if (!reg.test(res.userInfo.nickName)) {
                              res.userInfo.nickName == "默认用户名"
                            }
                            that.setData({
                              userInfo: res.userInfo
                            })
                            wx.request({
                              url: app.globalData.allUrl + "/login/login",
                              data: {
                                code: rese.code,
                              },
                              header: { 'content-type': "application/x-www-form-urlencoded" },
                              method: 'POST',
                              success: function (datas) {
                                wx.request({
                                  url: app.globalData.allUrl + "/login/saveUserInfo.php",
                                  data: {
                                    user_id: datas.data.openid,
                                    user_name: res.userInfo.nickName,
                                    image_url: res.userInfo.avatarUrl,
                                  //  pid: app.globalData.referrerId,
                                  },
                                  header: { 'content-type': "application/x-www-form-urlencoded" },
                                  method: 'POST',
                                  success: function (res) {
                                    wx.setStorageSync('userid', datas.data.openid)
                                  }, fail: function () {
                                  }
                                })
                              }, fail: function () {
                              }
                            })
                          }
                        })
                      }
                    }, fail: function (res) {
                    }
                  })
                }
              }
            })
          }
        })
      }
    })
  }

}